/**
 * (created at 2011-1-26)
 */
package qiushuo.mysqlparser.ast.statement;

import java.util.List;

import qiushuo.mysqlparser.ast.expression.Expression;
import qiushuo.mysqlparser.util.Pair;

/**
 * @author <a href="mailto:QiuShuo1985@gmail.com">QIU Shuo</a>
 */
public class DMLInsertStatement extends DMLStatement {
    public static final int LDH_UNDEF = 0;
    public static final int LDH_LOW = 1;
    public static final int LDH_DELAY = 2;
    public static final int LDH_HIGH = 3;

    private final int mode;
    private final boolean ignore;
    private final String tableName;
    private final List<String> columnNameList;
    private final List<List<Expression>> valuesList;
    private final Expression select;
    private final List<Pair<String, Expression>> duplicateUpdate;

    /**
     * (insert ... values | insert ... set) format
     * 
     * @param columnNameList can be null
     */
    @SuppressWarnings("unchecked")
    public DMLInsertStatement(int mode, boolean ignore, String tableName, List<String> columnNameList,
                              List<List<Expression>> valuesList, List<Pair<String, Expression>> duplicateUpdate) {
        this.valuesList = checkAndConvertValuesList(valuesList);
        this.select = null;
        this.mode = mode;
        this.ignore = ignore;
        this.tableName = tableName;
        this.columnNameList = ensureListType(columnNameList);
        this.duplicateUpdate = ensureListType(duplicateUpdate);
    }

    /**
     * insert ... select format
     * 
     * @param columnNameList can be null
     */
    @SuppressWarnings("unchecked")
    public DMLInsertStatement(int mode, boolean ignore, String tableName, List<String> columnNameList,
                              Expression select, List<Pair<String, Expression>> duplicateUpdate) {
        if (select == null) throw new IllegalArgumentException("argument 'select' is empty");
        this.select = select;
        this.valuesList = null;
        this.mode = mode;
        this.ignore = ignore;
        this.tableName = tableName;
        this.columnNameList = ensureListType(columnNameList);
        this.duplicateUpdate = ensureListType(duplicateUpdate);
    }

}
